package client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.rpc.*;

import java.util.*;

import client.UI_stuff.*;

/**
 * ContactListApp
 *
 * @author Masud Idris
 * @version 1.0
 * @since Dec 18, 2007, 9:20:41 PM
 */
public class ContactListApp implements EntryPoint {
  private ContactListServiceAsync contactListService;
  private RootPanel rootPanel;
  private VerticalPanel mainPanel;
  private Button button;


  /**
   * Implements the EntryPoint interface's onModuleLoad method
   */
  public void onModuleLoad() {
    contactListService = ContactListService.App.getInstance();
    rootPanel = RootPanel.get("Contact-List-App-Container");
    mainPanel = new VerticalPanel();
    button = new Button();

    DOM.setInnerHTML(RootPanel.get("Loading-Panel").getElement(), "");

    setupAndAddButton();
    rootPanel.add(mainPanel);
  }


  /**
   * Method used to setup and add the button to the app
   */
  public void setupAndAddButton() {
    button.setHTML("Get Contact List");
    button.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        if (mainPanel.getWidgetCount() > 0) {
          mainPanel.remove(0);
        }
        callContactListService();        
      }
    });

    rootPanel.add(button);
  }

  /**
   * Call the Contact List Service
   */
  public void callContactListService() {
    final HTML loading = new HTML("    <img src=\"loading.gif\" alt=\"loading\"> Loading Contact List , please wait...");
    mainPanel.add(loading);

    contactListService.getContactList(new AsyncCallback() {
      public void onFailure(Throwable caught) {
        Window.alert("Crap Shit Fuck!!");
      }

      public void onSuccess(Object result) {
        ContactListWidget contactListWidget = new ContactListWidget((ArrayList) result);
        mainPanel.add(contactListWidget);
        mainPanel.remove(loading);
      }
    });

  }
}
